/**
 * 
 */
package by.wind.drebedengi;

import java.util.concurrent.ExecutionException;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import by.wind.drebedengi.asynctask.AbstractTask;
import by.wind.drebedengi.asynctask.AsyncTaskManager;
import by.wind.drebedengi.asynctask.DeleteAsyncTask;
import by.wind.drebedengi.asynctask.OnTaskCompleteListener;
import by.wind.drebedengi.asynctask.ReportingAllAsyncTask;
import by.wind.drebedengi.asynctask.ReportingIncomingAsyncTask;
import by.wind.drebedengi.asynctask.ReportingOutgoingAsyncTask;
import by.wind.drebedengi.database.MyDatabaseHelper;
import by.wind.drebedengi.utils.RecordAdapter;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

/**
 * @author wind
 *
 */
public class ReportingActivity extends OrmLiteBaseActivity<MyDatabaseHelper>  implements OnTaskCompleteListener{
	
	private Button itsAllButton;
	private Button itsOutgoingButton;
	private Button itsIncomingButton;
	private ListView itsListView;
	
	private AsyncTaskManager itsAsyncTaskManager;
	
	private AbstractTask itsCurrentState;
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
    public void onCreate(Bundle savedInstanceState) {
        
		super.onCreate(savedInstanceState);
        setContentView(R.layout.report_tab);
        itsCurrentState = new ReportingAllAsyncTask(ReportingActivity.this); 
        
        itsAsyncTaskManager = new AsyncTaskManager(this, this);
		itsAsyncTaskManager.handleRetainedTask(getLastNonConfigurationInstance());
        
        itsAllButton = (Button) findViewById(R.id.buttonAllRecords);
        itsAllButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				itsCurrentState = new ReportingAllAsyncTask(ReportingActivity.this);
				itsAsyncTaskManager.setupTask(itsCurrentState);
			}
        });
        
        itsOutgoingButton = (Button) findViewById(R.id.buttonOutgoingRecords);
        itsOutgoingButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				itsCurrentState = new ReportingOutgoingAsyncTask(ReportingActivity.this);
				itsAsyncTaskManager.setupTask(itsCurrentState);
			}
        	
        });
        
        itsIncomingButton = (Button) findViewById(R.id.buttonIncomingRecords);
        itsIncomingButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				itsCurrentState = new ReportingIncomingAsyncTask(ReportingActivity.this);
				itsAsyncTaskManager.setupTask(itsCurrentState);
			}
        	
        });
	}
	
	public AbstractTask getState() {
		return itsCurrentState;
	}

	@Override
	public void onTaskComplete(by.wind.drebedengi.asynctask.AbstractTask task) {
		if (task.isCancelled()) {
		    // Report about cancel
		    Toast.makeText(this, R.string.task_cancelled, Toast.LENGTH_LONG)
			    .show();
		} else if (task instanceof ReportingAllAsyncTask){
			itsListView = (ListView) findViewById(R.id.listView1);
			RecordAdapter recordAdapter = new RecordAdapter(this,R.layout.listview_item_row, ((ReportingAllAsyncTask)task).getList());
			itsListView.setAdapter(recordAdapter);
			itsListView.setClickable(true);
			itsListView.setOnItemClickListener( new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterview, View view,
						int position, long id) {
					for(int i = 0; i<adapterview.getChildCount(); i++) {
						View viewChild = adapterview.getChildAt(i);
						viewChild.findViewById(R.id.lw_edit).setVisibility(View.GONE);
						viewChild.findViewById(R.id.lw_delete).setVisibility(View.GONE);
					}
					((Button) view.findViewById(R.id.lw_edit)).setVisibility(View.VISIBLE);
					((Button) view.findViewById(R.id.lw_delete)).setVisibility(View.VISIBLE);
				}
	    	});
			
		} else if (task instanceof DeleteAsyncTask) {
			// Task is not completed
			try {
				if (!task.get()) {
					AlertDialog.Builder b = new AlertDialog.Builder(this);
					b.setTitle(android.R.string.dialog_alert_title);
					b.setMessage(task.onErrorMessage());
					b.setPositiveButton(getString(android.R.string.ok),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dlg,
										int arg1) {
									dlg.dismiss();
								}
							});
					b.create().show();
				} else {
					itsAsyncTaskManager = new AsyncTaskManager(this, this);
					itsAsyncTaskManager
							.handleRetainedTask(getLastNonConfigurationInstance());
					itsAsyncTaskManager.setupTask(new ReportingAllAsyncTask(
							ReportingActivity.this));
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
    public Object onRetainNonConfigurationInstance() {
		return itsAsyncTaskManager.retainTask();
    }
	
	public AsyncTaskManager getAsyncTaskManager() {
		return itsAsyncTaskManager;
	}
}
